﻿using SwenAssigment.Classes;
using SwenAssigment.DBClasses;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SwenAssigment
{
    public partial class StaffEdit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request.QueryString["StaffId"] == null)
                {
                    Response.Redirect("~/StaffOverview.aspx");
                }
                Staff staff = StaffDBManager.GetStaffById(Convert.ToInt32(Request.QueryString["StaffId"]));
                IdLbl.Text = Request.QueryString["StaffId"];
                StaffNameTbx.Text = staff.StaffName;
                NRICTbx.Text = staff.NRIC;
                DobTbx.Text = staff.Dob.ToString("M/d/yyyy");
                BankAccountTbx.Text = staff.BankAccountNumber;
                AddressTbx.Text = staff.Address;
                ContactNumberTbx.Text = Convert.ToString(staff.ContactNumber);
                JobTbx.Text = staff.Job;

                if (StaffDBManager.GetStaffByGeneralDuty(Convert.ToInt32(Request.QueryString["StaffId"])) == true)
                {
                    DutyCBL.Items[0].Selected = true;
                }
                if (StaffDBManager.GetStaffByRoomDuty(Convert.ToInt32(Request.QueryString["StaffId"])) == true)
                {
                    DutyCBL.Items[1].Selected = true;
                }
                if (StaffDBManager.GetStaffByEstateDuty(Convert.ToInt32(Request.QueryString["StaffId"])) == true)
                {
                    DutyCBL.Items[2].Selected = true;
                }
                if (StaffDBManager.GetStaffBySecurityDuty(Convert.ToInt32(Request.QueryString["StaffId"])) == true)
                {
                    DutyCBL.Items[3].Selected = true;
                }
            }
        }

        protected void UpdateBtn_Click(object sender, EventArgs e)
        {
            Page.Validate();
            if (Page.IsValid)
            {
                String Format = "M/d/yyyy";
                String DateStr = DobTbx.Text;
                DateTime ParsedDate = DateTime.ParseExact(DateStr, Format, CultureInfo.InvariantCulture);
                var Staff = new Staff() { StaffId = Convert.ToInt32(IdLbl.Text), StaffName = StaffNameTbx.Text, Dob = ParsedDate, BankAccountNumber = BankAccountTbx.Text, Address = AddressTbx.Text, ContactNumber = Convert.ToInt32(ContactNumberTbx.Text), Job = JobTbx.Text, NRIC = NRICTbx.Text };
                StaffDBManager.UpdateStaff(Staff);

                if (DutyCBL.Items[0].Selected)
                {
                    if (StaffDBManager.GetStaffByGeneralDuty(Staff.StaffId) == false)
                    {
                        StaffDBManager.InsertStaffGeneralDuty(Staff.StaffId);
                    }
                }
                else
                {
                    if (StaffDBManager.GetStaffByGeneralDuty(Staff.StaffId) == true)
                    {
                        StaffDBManager.DeleteStaffByGeneralDuty(Staff.StaffId);
                    }
                }

                if (DutyCBL.Items[1].Selected)
                {
                    if (StaffDBManager.GetStaffByRoomDuty(Staff.StaffId) == false)
                    {
                        StaffDBManager.InsertStaffRoomDuty(Staff.StaffId);
                    }
                }
                else
                {
                    if (StaffDBManager.GetStaffByRoomDuty(Staff.StaffId) == true)
                    {
                        StaffDBManager.DeleteStaffByRoomDuty(Staff.StaffId);
                    }
                }

                if (DutyCBL.Items[2].Selected)
                {
                    if (StaffDBManager.GetStaffByEstateDuty(Staff.StaffId) == false)
                    {
                        StaffDBManager.InsertStaffEstateDuty(Staff.StaffId);
                    }
                }
                else
                {
                    if (StaffDBManager.GetStaffByEstateDuty(Staff.StaffId) == true)
                    {
                        StaffDBManager.DeleteStaffByEstateDuty(Staff.StaffId);
                    }
                }

                if (DutyCBL.Items[3].Selected)
                {
                    if (StaffDBManager.GetStaffBySecurityDuty(Staff.StaffId) == false)
                    {
                        StaffDBManager.InsertStaffSecurityDuty(Staff.StaffId);
                    }
                }
                else
                {
                    if (StaffDBManager.GetStaffBySecurityDuty(Staff.StaffId) == true)
                    {
                        StaffDBManager.DeleteStaffBySecurityDuty(Staff.StaffId);
                    }
                }
                Response.Redirect("~/StaffSuccessfulUpdate.aspx");
            }
        }

        protected void DeleteBtn_Click(object sender, EventArgs e)
        {
            Page.Validate();
            if (Page.IsValid)
            {
                if (StaffDBManager.GetStaffByGeneralDuty(Convert.ToInt32(IdLbl.Text)) == true)
                {
                    StaffDBManager.DeleteStaffByGeneralDuty(Convert.ToInt32(IdLbl.Text));
                }
                if (StaffDBManager.GetStaffByRoomDuty(Convert.ToInt32(IdLbl.Text)) == true)
                {
                    StaffDBManager.DeleteStaffByRoomDuty(Convert.ToInt32(IdLbl.Text));
                }
                if (StaffDBManager.GetStaffByEstateDuty(Convert.ToInt32(IdLbl.Text)) == true)
                {
                    StaffDBManager.DeleteStaffByEstateDuty(Convert.ToInt32(IdLbl.Text));
                }
                if (StaffDBManager.GetStaffBySecurityDuty(Convert.ToInt32(IdLbl.Text)) == true)
                {
                    StaffDBManager.DeleteStaffBySecurityDuty(Convert.ToInt32(IdLbl.Text));
                }
                StaffDBManager.DeleteStaff(Convert.ToInt32(IdLbl.Text));
                Response.Redirect("~/StaffSuccessfulDelete.aspx");
            }
        }

    }
}